/*
Copyright (c) 2020, Huawei Technologies Co., Ltd
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* Description: 2D图表可视化插件数据组织类
*/
#ifndef ST_SPACE_DATA_H
#define ST_SPACE_DATA_H

#include <QPolygonF>
#include <QMap>
#include <QSharedPointer>

#include "custom/StSpace.h"


namespace chart_plugin {
class StObject {
public:
    StObject(const QString &objectId) {
        this->objectId = objectId;
        this->stObjectId = QString::number(id++);
    };
    ~StObject() {};

    void setId(int32_t id) {
        this->id = id;
        this->stObjectId = QString::number(this->id++);
    }

    QString stObjectId;         // 程序生成的id
    QString objectId;           // 数据包传入的id
    bool isVisible { true };    // 是否可见
    static uint32_t id;         // id
};
class StSpaceData {
public:
    StSpaceData() = default;
    ~StSpaceData() = default;

    /*
     * 功能：添加topic StSpace数据
     * 输入参数：msg-custom::StSpace::ConstPtr
     * 输出参数：无
     * 返回值：true-成功，false-添加数据失败
     */
    bool Add(const custom::StSpace::ConstPtr &msg);

    /*
     * 功能：清除缓存的topic StSpace数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：无
     */
    void Clear();

    /*
     * 功能：获取First St Track曲线数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QPolygonF曲线数据
     */
    const QPolygonF &GetFirstStTrack() const;

    /*
     * 功能：获取Optimized St Track曲线数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QPolygonF曲线数据
     */
    const QPolygonF &GetOptimizedStTrack() const;

    /*
     * 功能：获取St Speed Limit曲线数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QPolygonF曲线数据
     */
    const QPolygonF &GetStSpeedLimit() const;

    /*
     * 功能：获取障碍物曲线数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QMap障碍物数据，障碍物唯一id和其曲线数据QPolygonF
     */
    const QMap<QString, QPolygonF> &GetObjectsMap() const;

    /*
     * 功能：获取障碍物属性数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QMap障碍物属性数据，障碍物id和其对应type信息
     */
    const QMap<QString, int32_t> &GetObjectsIdTypeMap() const;
	
    /*
     * 功能：获取障碍物属性数据
     * 输入参数：无
     * 输出参数：无
     * 返回值：QMap障碍物属性数据，障碍物type和其对应id信息
     */
    const QMap<int32_t, QStringList> &GetObjectsTypeIdMap() const;

    /*
     * 功能：设置障碍物是否显示
     * 输入参数：id-障碍物id属性，isVisible-障碍物显隐
     * 输出参数：无
     * 返回值：无
     */
    void SetObjectVisible(const QString &id, bool isVisible);
    
    /*
     * 功能：获取障碍物是否显示
     * 输入参数：id-障碍物id属性
     * 输出参数：无
     * 返回值：障碍物是否显示
     */
    bool GetObjectVisible(const QString &id) const;

private:
    /*
     * 功能：设置曲线QPolygonF数据信息
     * 输入参数：pointVector-订阅得到的topic数据中的曲线的StPoint数据
     * 输出参数：qPolygonF-曲线的QPolygonF数据
     * 返回值：true-成功，false-失败
     */
    bool SetCurveData(const std::vector<custom::StPoint> &pointVector, QPolygonF *qPolygonF) const;

    /*
     * 功能：设置障碍物曲线QPolygonF数据信息
     * 输入参数：pointVector-订阅得到的topic数据中的障碍物曲线的StPoint数据
     * 输出参数：无
     * 返回值：无
     */
    void SetObjectsCurveData(const std::vector<custom::StPoint> &pointVector);

    /*
     * 功能：设置idObjectMap上限
     * 输入参数：listId-障碍物链表
     * 输出参数：无
     * 返回值：无
     */
    void LimitObjectsNumber(const QStringList &listId);
	
    /*
     * 功能：设置id大小上限
     * 输入参数：无
     * 输出参数：无
     * 返回值：无
     */
    void LimitIdSize();

private:
    QPolygonF firstStTrack {};
    QPolygonF optimizedStTrack {};
    QPolygonF stSpeedLimit {};

    QMap<QString, QPolygonF> objectsMap {};
    QMap<QString, int32_t> objectsIdTypeMap {};
    QMap<int32_t, QStringList> objectsTypeIdMap {};

    QMap<QString, QSharedPointer<StObject>> idObjectMap {}; //stObjectId(程序生成的id),object
    QMap<QString, QString> idIdMap {};                       // objectId(topic读取的id), stObjectId(程序生成的id)
};
} // namespace chart_plugin

#endif // ST_SPACE_DATA_H
